//
//  KZAlertView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/31.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZAlertView.h"

#import "KZShadowButton.h"

@implementation KZAlertView
{
    NSString *_title;
    NSAttributedString *_subtitle;
    NSString *_warningtitle;
    NSArray *_submitTypes;
    void (^_config)(NSArray *buttons);
    void (^_click)(NSInteger index);
    NSArray *_indexs;
}

- (void)dealloc {
    NSLog(@"释放了");
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 8;
    }
    return self;
}


- (void)setupUI {
    
    UIImageView *iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:@"LpkLS.bundle/kz-鸽子集"];
    [self addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.centerY.equalTo(self.mas_top).offset(15);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = _title;
    titleLabel.font = B_FONT(20);
    titleLabel.text = @"温馨提示";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    [self addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(iconView.mas_bottom).offset(-10);
    }];
    
    UILabel *subtitleLabel = [[UILabel alloc] init];
    subtitleLabel.font = FONT(14);
    subtitleLabel.numberOfLines = 0;
    subtitleLabel.textAlignment = NSTextAlignmentCenter;
    subtitleLabel.textColor = [UIColor blackColor];
    subtitleLabel.attributedText = _subtitle;
    [self addSubview:subtitleLabel];
    [subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(20));
        make.right.mas_equalTo(W(-20));
        make.top.equalTo(titleLabel.mas_bottom).offset(20);
        // 高度自适应

        // 没有按钮
        if (_submitTypes.count == 0) {
            make.bottom.equalTo(@-20);
        }
    }];
    
    __block NSMutableArray *temp = [NSMutableArray new];
    
    for (int i = 0; i < _submitTypes.count; i ++) {
        
        int type = [_submitTypes[i] intValue];
        UIButton *button;
        
        if (type == 0) {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.layer.cornerRadius = H(18);
            button.layer.masksToBounds = true;
            button.layer.borderColor = kBlueColor.CGColor;
            button.layer.borderWidth = 1;
            button.titleLabel.font = FONT(15);
            [button setTitle:@"" forState:UIControlStateNormal];
            [button setTitleColor:kBlueColor forState:UIControlStateNormal];
            [temp addObject:button];
        } else {
            button = [[KZShadowButton alloc] init:^(UILabel *label) {
                [temp addObject:label];
            }];
        }
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(H(36));
            make.width.equalTo(self).multipliedBy(0.4);
            make.top.equalTo(subtitleLabel.mas_bottom).offset(30);
            if (_submitTypes.count == 1) {
                make.centerX.equalTo(@0);
            } else if (_submitTypes.count == 2) {
                make.centerX.equalTo(self.mas_centerX).multipliedBy(i == 0 ? (8.0/15) : 22.0/15);
            }
            if (i == 0) {
                make.bottom.equalTo(@-20);
            }
        }];
        
    }
    if (_config) {
        _config(temp.copy);
    }
}

- (void)buttonAction:(UIButton *)button {
    
    BOOL netValid = [[NSUserDefaults.standardUserDefaults objectForKey:@"KZNetValid"] boolValue];
    if (netValid == false) {
//        [MBProgressHUD showFailureWithText:@"请检查网络"];
        return;
    }
//    AppDelegate *delegete = (AppDelegate *)UIApplication.sharedApplication.delegate;
//    if (delegete.netValid == false) {
//        [MBProgressHUD showFailureWithText:@"请检查网络"];
//        return;
//    }
    
    [_indexs enumerateObjectsUsingBlock:^(NSNumber *index, NSUInteger idx, BOOL * _Nonnull stop) {
        if (button.tag == index.integerValue) {
            *stop = true;
            [self hide];
        }
    }];
    if (_click) {
        _click(button.tag);
    }
}

- (void)showWithTitle:(NSString *)title
             subtitle:(NSAttributedString *)subtitle
          submitTypes:(NSArray *)submitTypes
  submitButtonsConfig:(void(^)(NSArray<UIButton *> *buttons))config
                click:(void(^)(NSInteger index))click
  destroyAtClickIndex:(NSArray *)indexs {
    
    [self showWithTitle:title subtitle:subtitle warningtitle:nil submitTypes:submitTypes submitButtonsConfig:config click:click destroyAtClickIndex:indexs];
}

- (void)showWithTitle:(NSString *)title
             subtitle:(NSAttributedString *)subtitle
         warningtitle:(NSString *)warningtitle
          submitTypes:(NSArray *)submitTypes
  submitButtonsConfig:(void(^)(NSArray<UIButton *> *buttons))config
                click:(void(^)(NSInteger index))click
  destroyAtClickIndex:(NSArray *)indexs {
    
    _title = title;
    _subtitle = subtitle;
    _warningtitle = warningtitle;
    _submitTypes = submitTypes;
    _config = config;
    _click = click;
    _indexs = indexs;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIView *maskView = [UIView new];
    maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.0];
    [window addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
    maskView.tag = 8023;
    
    [maskView addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(maskView).multipliedBy(0.8);
        make.center.equalTo(maskView);
        // 高度根据子视图
    }];
    
    [self layoutIfNeeded];
    [self setupUI];
    
    window.userInteractionEnabled = false;
    self.transform = CGAffineTransformMakeScale(0.001, 0.001);
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformIdentity;
        maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
        window.userInteractionEnabled = true;
    }];
}

- (void)hide {

    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformMakeScale(0.001, 0.001);
        self.superview.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.1];

    } completion:^(BOOL finished) {
        if (finished) {
            [self.superview removeFromSuperview];
        }
    }];
}
@end
